<?php

namespace app\admin\controller;

use app\common\controller\AdminController;

//引入外部函数
use app\common\model\hfztcy\LibStaffBalanceRecords;


/**
 * 佣金记录
 * @name AdminStaffBalanceRecords.php
 * @package hfztcy
 * @category controller
 * @author rxy
 * @version 1.0
 * @copyright CHANGE INC
 * @since 2020-6-5
 */
class AdminStaffBalanceRecords extends AdminController
{
    protected $model;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new LibStaffBalanceRecords();
        $this->rightVerify(session('admin'), url("admin/Login/login"));
        parent::__construct();
    }

    /**
     * 部门员工-列表页
     */
    function balanceList()
    {
        $this->getSetMenu($this);
        $this->assign('state',5);
        $this->log(__CLASS__, __FUNCTION__, "列表页", 1, 'view');
        return $this->fetch('BalancesRecords/adminStaffBalanceList/balanceList');
    }

    /**
     * 添加页面
     */
    function addBalance()
    {
        $this->getMenu($this);
        $this->log(__CLASS__, __FUNCTION__, "添加页面", 1, 'view');
        return $this->fetch('BalancesRecords/addBalance/addBalance');
    }

    /**
     * 编辑页面
     */
    function editBalance()
    {
        $this->getMenu($this);
        $balanceInfo = $this->model->findCommissionRecords(['id' => input("id")]);
        $this->assign('balanceInfo', $balanceInfo['data']);
        $this->log(__CLASS__, __FUNCTION__, "编辑页面", 1, 'view');
        return $this->fetch('BalancesRecords/editBalance/editBalance');
    }

    /**
     * 删除
     */
    function deleteBalance()
    {
        $id = input("id");
        $result = $this->model->deleteCommissionRecords($id);
        echo json_encode($result);
    }

    /**
     * 分页查询
     */
    function pagingBalance()
    {
        $args = $this->getArgsList($this, ['username', 'user_id', 'zj_state','state']);
        $page = $this->getPageInfo();
        $where = [];
        $where[] = "`a`.`state` =  {$args['state']}";
        if (!empty($args['username'])) $where[] = "`a`.`username` like '%" . $args['username'] . "%'";
        if (!empty($args['user_id'])) {
            $where[] = "`a`.`user_id` = '" . $args['user_id'] . "'";
        } else {
            $user_id = session('admin.id');
            $group_id = session('admin.group_id');
            if (!$group_id) {
                $adminIds = db('base_admin')->where('group_id','=',3)->column('id');
                $adminIds = implode(',',$adminIds);
                $where[] = "`a`.`user_id` IN ($adminIds)";
            } else {
                $where[] = "`a`.`user_id` = '" . $user_id . "'";
            }
        }
        if (($args['zj_state'] == 0 || $args['zj_state'] == 1 || $args['zj_state'] == 2) && $args['zj_state'] != '') $where[] = "`a`.`zj_state` = '" . $args['zj_state'] . "'";
        $result = $this->model->pagingCommissionRecords($page, implode(' AND ', $where));
        foreach ($result['data']['dataList'] as &$item) {
            $item['order_info'] = json_decode($item['order_info'],true);
        }
        echo json_encode($result);
    }
}

?>